<?php

namespace app\astrologer\controller\v8;

use app\astrologer\controller\AstrAuth;
use app\common\cachemodel\zlcr3\Zlcr3AstrModifyCheck;
use app\common\cachemodel\zlcr3\Zlcr3ConsultAccount;
use app\common\cachemodel\ZlcrAstr;
use think\response\Json;

class User extends AstrAuth
{
    protected $defaultChat = "Hi，很高兴为你服务！\r\n你可以和我聊聊你的疑惑，或者直接向我咨询。";//todo 默认对话内容

    /**
     * 主页
     * @return Json
     * @throws \Exception
     */
    public function homePage()
    {
        $res = (new ZlcrAstr())->homePage2($this->astrInfo, 2);
        unset($res['status']);
        $res['level'] = $this->isConsultAstro ? $this->consultAccountInfo['level'] : 0;

        return show(config('code.success'), '', $res);
    }

    /**
     * 获取默认回复和首次对话信息
     * @return \think\response\Json
     */
    public function defaultChat()
    {
        $extend = $this->astrInfo['extend'];
        $checkInfo = (new Zlcr3AstrModifyCheck())->checkTypeInfo($this->astrInfo['id']);

        $data = [
            'content' => !empty($checkInfo['default_chat']) ? $checkInfo['default_chat'] : ($extend['default_chat'] ?? $this->defaultChat),
            'reply_content' => !empty($checkInfo['default_reply']) ? $checkInfo['default_reply'] : ($extend['default_reply'] ?? ''),
            'content_status' => !empty($checkInfo['default_chat']) ? 1 : 0,//0=>正常    1=>审核中
            'reply_content_status' => !empty($checkInfo['default_reply']) ? 1 : 0 //0=>正常    1=>审核中
        ];
        return show(config('code.success'), 'success', $data);
    }

    /**
     * 设置默认回复和首次对话信息
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setDefaultChat()
    {
        $type = input('type/d', 1);//1=>更新对话信息  2=>恢复默认对话信息
        $content = input('content/s', '');
        $contentType = input('content_type/d', 0);//1=>首次默认对话消息  2=>默认回复消息
        if (!in_array($contentType, [1, 2], true))
        {
            return show(config('code.error'), '参数错误');
        }
        $extend = $this->astrInfo['extend'];

        if ($contentType === 1)
        {
            if ($type === 1)
            {
                if (!$content)
                {
                    return show(config('code.error'), '请输入内容');
                }
                $checkType = 10;
                $checkExtend['default_chat'] = $content;
            } else
            {
                $extend['default_chat'] = $this->defaultChat;//默认对话内容
            }
        } else
        {
            if ($type === 1)
            {
                if (!$content)
                {
                    return show(config('code.error'), '请输入内容');
                }
                $checkType = 11;
                $checkExtend['default_reply'] = $content;
            } else
            {
                $extend['default_reply'] = '';//默认回复内容
            }
        }

        if (isset($checkExtend))
        {
            $checkModel = new Zlcr3AstrModifyCheck();
            $newCheckId = $checkModel->where(['astr_id' => $this->astrInfo['id'], 'type' => $checkType, 'status' => 0])->order('id', 'DESC')->field('id')->find();
            if ($newCheckId)
            {
                $checkModel->where('id', $newCheckId['id'])->update(['status' => 3, 'update_time' => time()]);
            }

            $checkModel->insert([
                'status' => 0,
                'astr_id' => $this->astrInfo['id'],
                'type' => $checkType,
                'extend' => json_encode($checkExtend, true),
                'create_time' => time()
            ]);
        } elseif ($type === 2)
        {
            $data['extend'] = json_encode($extend, JSON_UNESCAPED_UNICODE);
            (new ZlcrAstr())->UpdateData($this->astrInfo['id'], ['id' => $this->astrInfo['id']], $data);
        }

        return show(config('code.success'), 'success');
    }

    /**
     * 修改营业状态
     * @return Json
     */
    public function alterWorkStatus()
    {
        $status = input('status/d', 0);
        if (!in_array($status, [1, 2], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $zlcr3ConsultAccountModel = new Zlcr3ConsultAccount();
        $consultAccountData = $zlcr3ConsultAccountModel->getInfoById($this->astrInfo['id']);
        if ($status === $consultAccountData['status'])
        {
            return show(config('code.success'), '请求成功');
        }
        if ($status === 1)
        {
            $complete = isset($this->astrInfo['extend']['is_complete_information']) ? 1 : 0;
            if ($complete === 0)
            {
                return show(config('code.error'), '请先完善资料');
            }
            if (!$this->astrInfo['tel'])
            {
                return show(config('code.error'), '请绑定手机号');
            }
            if (!$this->astrInfo['uid'])
            {
                return show(config('code.error'), '请绑定准了号');
            }
        }
        // 开启营业
        $zlcr3ConsultAccountModel->updateData($this->astrInfo['id'], ['status' => $status]);

        return show(config('code.success'), '修改成功');
    }
}
